<?php
function is_valid_email($email)
{
	if(preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
		return true;
	else
		return false;
}
$cparams =& JComponentHelper::getParams('com_media');
$app = JFactory::getApplication();
$templateDir = JURI::base() . 'templates/' . $app->getTemplate(); 
$sendemail=false;
if($_POST['submit'])
{
	$sendemail = true;
	if(!is_valid_email($_POST['from']))
	{
		$error[]['text']=JText::_('PLEASE INPUT EMAIL');
		$sendemail=false;
	}
	if(!is_valid_email($_POST['to']))
	{
		$error[]['text']=JText::_('PLEASE INPUT EMAIL TO');
		$sendemail=false;
	}
	if($_POST['content']=="")
	{
		$error[]['text']=JText::_('PLEASE INPUT INVITE CONTENT');
		$sendemail=false;
	}
}
if($sendemail)
{
	$lang =& JFactory::getLanguage();
	$locales = $lang->getLocale();
	if(strstr($locales[0],'vi'))
	{
		$language="vi";
	}
	else
	{
		$language="en";
	}
	$template = JPATH_ROOT.'/emails/'.$language.'/invite.php';
	$body=file_get_contents($template);

	$mailfrom = $_POST['from'];
	$email = $_POST['to'];
	$subject = "Cộng đồng học anh văn trực tuyến";
	$body=str_replace("{email}",$email, $body);
	$body=str_replace("{from}",$mailfrom, $body);
	$body=str_replace("{content}",$_POST['content'] , $body);
	$body=str_replace("{year}",date("Y"), $body);
	JUtility::sendMail($mailfrom,$mailfrom, $email, $subject, $body,1);
	$error[]['text']=JText::_('EMAIL HAS BEEN SENT');
}
?>
<script type="text/javascript">
function check_text_onfocus(obj)
{ if(obj.value == obj.defaultValue)obj.value = '';}
function check_text_onblur(obj){
if(obj.value=='')
obj.value = obj.defaultValue;
}
</script>
<link rel="stylesheet" type="text/css" href="<?php echo $templateDir; ?>/css/vita_subpages.css" media="screen" />
<div class="content">
                        <div class="innerContent"> 
                            <h2><span><span><?php echo JText::_('INVITE');?></span></span></h2>
                            <div class="fckDefault">
                              <a href="<?php echo JRoute::_('index.php?option=com_content&view=article&id=47&Itemid=54');?>" title="<?php echo JText::_('RETURN VITA');?>" class="lnkReturn"><img src="<?php echo $templateDir; ?>/images/vita/dummy.gif" alt="Return to Vita" class="uiIcon initIcon-09"/><span><?php echo JText::_('RETURN VITA');?></span></a> <br/>
                              <div id="infoBlock"><?php echo JText::_('INVITE INFO');?></div>
                               <form id="inviteForm" name="inviteForm" method="post">
                              <table class="contentForm">
                                <tr>
                                  <th><?php echo JText::_('EMAIL TEMPLATE');?></th>
                                </tr>
                                <?php
                                if(count($error)>0)
                                {
                                ?>
                                <tr>
                                	<td>
                                	<font color="red">
                                	<?php 
                                	foreach($error as $detail_err)
                                	{
                                		echo $detail_err['text'].'<br/>';
                                	}
                                	?>
                                	</font>
                                	</td>
                                </tr>
                                <?php
                                } 
                                ?>
                                <tr>
                                	<td>
                                		<?php echo JText::_('FROM');?>: <input name="from" type='text' size="35" value='<?php echo JText::_('PLEASE INPUT EMAIL');?>' onfocus='javascript:check_text_onfocus(this)' onblur='javascript:check_text_onblur(this)' style="border:none; color: #bbb;" />
                                		<br/>                                		                              
                                		<?php echo JText::_('TO');?>: <input name="to" type='text' size="35" value='<?php echo JText::_('PLEASE INPUT EMAIL TO');?>' onfocus='javascript:check_text_onfocus(this)' onblur='javascript:check_text_onblur(this)' style="border:none; color: #bbb;" /></br>
                                		<?php echo JText::_('SUBJECT');?>: <?php echo JText::_('INVITE SUBJECT');?><br/><br/>
                                		<textarea name="content" id="content" rows="6" cols="100" style="border:none; color: #bbb;"><?php echo JText::_('INVITE CONTENT');?>
                                		</textarea>
                                	</td>
                                </tr>
                                
                                <tr>
                                	<td class="sendForm"><input id="submit" name="submit" type="submit" value="<?php echo JText::_('SEND');?>" /></td>
                                </tr>
                              </table>
                              </form>
                            </div>
                        </div>
                </div>
